package com.lanou.connotationjoke.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.google.gson.Gson;
import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.view.RefreshableView;
import com.lanou.connotationjoke.activity.WebViewActivity;
import com.lanou.connotationjoke.adapter.ReviewGameListViewAdapter;
import com.lanou.connotationjoke.entity.AppUrl;
import com.lanou.connotationjoke.entity.ReviewDetialEty;
import com.lanou.connotationjoke.entity.ReviewEty;
import com.lanou.connotationjoke.tool.MyAppllication;
import com.lanou.connotationjoke.tool.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/23.
 */
public class ReviewGameFragment extends BaseFragment{
    private final String URL = AppUrl.ReviewGame;
    private RefreshableView refreshableView;
    private ListView listView;
    private View headView;
    private ReviewGameListViewAdapter adapter;
    private List<ReviewDetialEty> datas;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            Toast.makeText(getActivity(), "加载数据失败", Toast.LENGTH_SHORT).show();
            return false;
        }
    });
    private String headViewUrl;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_review_game,null);
        refreshableView = (RefreshableView) view.findViewById(R.id.refresh);
        listView = (ListView) view.findViewById(R.id.listview_review_game);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        VolleySingleton.addRequest(URL, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                ReviewEty reviewEty = gson.fromJson(response, ReviewEty.class);
                datas = reviewEty.getT1348654151579();
                initHeadview();
                listView.addHeaderView(headView);
                headViewUrl = datas.get(0).getUrl_3w();
                datas.remove(0);
                adapter = new ReviewGameListViewAdapter(getActivity()
                        , datas);
                listView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                handler.sendEmptyMessage(0);
            }
        });


        refreshableView.setOnRefreshListener(new RefreshableView.PullToRefreshListener() {
            @Override
            public void onRefresh() {

                VolleySingleton.addRequest(URL, new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        ReviewEty reviewEty = gson.fromJson(response, ReviewEty.class);
                        datas = reviewEty.getT1348654151579();
                        adapter.refreshData(datas);
                        refreshableView.finishRefreshing();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        handler.sendEmptyMessage(0);
                        refreshableView.finishRefreshing();
                    }
                });
            }
        }, 3);


            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (MyAppllication.hasNetwork()){
                        if (position == 0){
                            WebViewActivity.goToWebViewActivity(getActivity()
                                    ,headViewUrl);
                        }else {
                            WebViewActivity.goToWebViewActivity(getActivity()
                                    ,datas.get(position - 1).getUrl_3w());
                        }

                    }else {
                        Toast.makeText(getActivity(), "没有网络", Toast.LENGTH_SHORT).show();
                    }
                }
            });



    }

    private void initHeadview(){
        headView = LayoutInflater.from(getActivity())
                .inflate(R.layout.headview_listview_review_game, null);
        NetworkImageView imageView =
                (NetworkImageView) headView.findViewById(R.id.iv_headview_game);
        TextView textView = (TextView) headView.findViewById(R.id.tv_headview_game);
        NetworkImageView picture1 = (NetworkImageView) headView.findViewById(R.id.iv_headview_game_picture1);
        NetworkImageView picture2 = (NetworkImageView) headView.findViewById(R.id.iv_headview_game_picture2);

        imageView.setDefaultImageResId(R.mipmap.ic_launcher);
        imageView.setErrorImageResId(R.mipmap.ic_launcher);
        imageView.setImageUrl(datas.get(0).getImgsrc(), VolleySingleton.getImageLoader());
        textView.setText(datas.get(0).getTitle());

        picture1.setDefaultImageResId(R.mipmap.ic_launcher);
        picture1.setErrorImageResId(R.mipmap.ic_launcher);
        picture1.setImageUrl(datas.get(7).getImgsrc(), VolleySingleton.getImageLoader());

        picture2.setDefaultImageResId(R.mipmap.ic_launcher);
        picture2.setErrorImageResId(R.mipmap.ic_launcher);
        picture2.setImageUrl(datas.get(9).getImgsrc(), VolleySingleton.getImageLoader());
    }

}
